package com.nc.delivery_project.server.service;

import com.nc.delivery_project.server.dao.AbstractDao;
import com.nc.delivery_project.server.dao.CargoDao;
import com.nc.delivery_project.server.db_util.ConnectionManager;
import com.nc.delivery_project.server.domain.Cargo;
import com.nc.delivery_project.server.domain.Currency;
import com.nc.delivery_project.server.util.CRUD;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: Natalie D.
 * Date: 1/12/15
 * Time: 6:11 PM
 */
public class CargoService extends BaseService<Cargo> {
   // private CargoDao cargoDao;
    private CurrencyService currencyService;
    public CargoService() {
        this(new CargoDao());
    }

    public CargoService(Connection connection, boolean isAutoCloseConnection) {
        super(new CargoDao(), connection, isAutoCloseConnection);
    }

    public CargoService(AbstractDao<Cargo> dao) {
        super(dao);
    }
    @Override
    public Cargo read(int id)
    {
        Cargo cargo = null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            cargo = dao.read(id);
            if (cargo != null)
            {
                Currency currency = new CurrencyService().read(cargo.getCurrency().getId());
                if (currency != null)
                {
                    cargo.setCurrency(currency);
                }
            }
        }
        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return cargo;
    }

    @Override
    public List<Cargo> read()
    {
        List<Cargo> cargos = null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            cargos = dao.read();
            currencyService = new CurrencyService();
            for (Cargo cargo : cargos)
                cargo.setCurrency(currencyService.read(cargo.getCurrency().getId()));
        }

        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cargos;
    }

    public List<Cargo> readByOrderId(int id)
    {
        List<Cargo> cargos = null;
        try
        {
            Connection conn = getConnection();
            dao.setConnection(conn);
            cargos = ((CargoDao)dao).readByOrderId(id);
            currencyService = new CurrencyService();
            for (Cargo cargo : cargos)
                cargo.setCurrency(currencyService.read(cargo.getCurrency().getId()));
        }

        catch (SQLException e) {
            e.printStackTrace();
        } finally {
            try {
                closeConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

        return cargos;
    }
}
